﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Restaurant.DTO;
using Restaurant.BUS;

namespace Restaurant.User_Control
{
    public partial class QLBan : UserControl
    {
        NhaHangService nhService = new NhaHangService();
        KhuVucService kvService = new KhuVucService();
        BanService bService = new BanService();
        private List<KhuVuc> listKhuVuc = new List<KhuVuc>();
        public bool fix = false;

        public QLBan()
        {
            InitializeComponent();
        }


        public void LoadTinhTrang()
        {
            cbbTinhTrang.Items.Add("Đã Đặt");
            cbbTinhTrang.Items.Add("Trống");
            cbbTinhTrang.Items.Add("Đang Sử Dụng");
            cbbTinhTrang.Text = cbbTinhTrang.Items[1].ToString();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            frmThemBan _frmThemBan = new frmThemBan();
            _frmThemBan.ShowDialog();
            int maNH = nhService.LayMaNH(cbbLocNH.Text);
            int maKhu = LayMaKhu(maNH);
            int maLoai = LayMaLoaiBan();
            int tinhtrang = LayMaTinhTrang();
            if (maKhu != -1)
                LoadBan(maNH, maKhu, maLoai, tinhtrang);
            else
                return;
        }

        private void button2_Click(object sender, EventArgs e)
        {
            int index = int.Parse(lvBan.FocusedItem.Index.ToString());
            if (index >= 0)
            {
                Ban b = bService.TimBanID(int.Parse(lvBan.Items[index].Text));
                frmThemBan frm = new frmThemBan();
                frm.B = b;
                frm.flag = 1;
                frm.ShowDialog();
                int maNH = nhService.LayMaNH(cbbLocNH.Text);
                int maKhu = LayMaKhu(maNH);
                int maLoai = LayMaLoaiBan();
                int tinhtrang = LayMaTinhTrang();
                if (maKhu != -1)
                    LoadBan(maNH, maKhu, maLoai, tinhtrang);
                else
                    return;
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            int index = lvBan.FocusedItem.Index;
            if (index >= 0)
            {
                int maBan = int.Parse(lvBan.Items[index].Text);
                if (bService.XoaBan(maBan))
                {
                    int maNH = nhService.LayMaNH(cbbLocNH.Text);
                    int maKhu = LayMaKhu(maNH);
                    int maLoai = LayMaLoaiBan();
                    int tinhtrang = LayMaTinhTrang();
                    if (maKhu != -1)
                        LoadBan(maNH, maKhu, maLoai, tinhtrang);
                    return;
                }
                else
                {
                    MessageBox.Show("Thất Bại");
                    return;
                }
            }
        }

        private void button4_Click(object sender, EventArgs e)
        {
            if (lvBan.FocusedItem.Index >= 0)
            {
                int index = lvBan.FocusedItem.Index;
                string tinhtrang = lvBan.Items[index].SubItems[4].Text;
                if (tinhtrang == "Trống")
                {
                    Ban b = bService.TimBanID(int.Parse(lvBan.Items[index].Text));
                    NhaHang nh = new NhaHang();
                    nh.TenNH = lvBan.Items[index].SubItems[5].Text;
                    nh.MaNH = nhService.LayMaNH(lvBan.Items[index].SubItems[5].Text);
                    frmDatCho frm = new frmDatCho();
                    frm.B = b;
                    frm.Nh = nh;
                    frm.ShowDialog();
                    int maNH = nhService.LayMaNH(cbbLocNH.Text);
                    LoadKhuTheoNH(maNH);
                    int maKhu = LayMaKhu(maNH);
                    int maLoai = LayMaLoaiBan();
                    int tinhtrang2 = LayMaTinhTrang();
                    if (maKhu != -1)
                        LoadBan(maNH, maKhu, maLoai, tinhtrang2);
                    else
                        return;
                }
                if(tinhtrang == "Đã Đặt")
                {

                }
                else
                {
                    return;
                }
            }
        }

        private void btnTroVe_Click(object sender, EventArgs e)
        {

        }

        private void cbbLocNH_SelectedIndexChanged(object sender, EventArgs e)
        {
            int maNH = nhService.LayMaNH(cbbLocNH.Text);
            LoadKhuTheoNH(maNH);
            int maKhu = LayMaKhu(maNH);
            int maLoai = LayMaLoaiBan();
            int tinhtrang = LayMaTinhTrang();
            if (maKhu != -1)
                LoadBan(maNH, maKhu, maLoai, tinhtrang);
            else
                return;
        }

        private void cbbKhuVuc_SelectedIndexChanged(object sender, EventArgs e)
        {
            int maNH = nhService.LayMaNH(cbbLocNH.Text);
            int maKhu = LayMaKhu(maNH);
            int maLoai = LayMaLoaiBan();
            int tinhtrang = LayMaTinhTrang();
            if (maKhu != -1)
                LoadBan(maNH, maKhu, maLoai, tinhtrang);
            else
                return;
        }

        private void cbbLoaiBan_SelectedIndexChanged(object sender, EventArgs e)
        {
            int maNH = nhService.LayMaNH(cbbLocNH.Text);
            int maKhu = LayMaKhu(maNH);
            int maLoai = LayMaLoaiBan();
            int tinhtrang = LayMaTinhTrang();
            if (maKhu != -1)
                LoadBan(maNH, maKhu, maLoai, tinhtrang);
            else
                return;
        }

        private void cbbTinhTrang_SelectedIndexChanged(object sender, EventArgs e)
        {
            int maNH = nhService.LayMaNH(cbbLocNH.Text);
            int maKhu = LayMaKhu(maNH);
            int maLoai = LayMaLoaiBan();
            int tinhtrang = LayMaTinhTrang();
            if (maKhu != -1)
                LoadBan(maNH, maKhu, maLoai, tinhtrang);
            else
                return;
        }

        private void LoadTatCaBan(int maNH, int maKhu, int loai, int tinhtrang)
        {
            DataTable dbtab = bService.TimBanTheoKhuNHLoaiTinhTrang(maNH, maKhu, loai, tinhtrang);
            lvBan.Items.Clear();
            for (int i = 0; i < dbtab.Rows.Count; i++)
            {
                DataRow dbrow = dbtab.Rows[i];
                ListViewItem liv = new ListViewItem();
                liv.Text = dbrow["MaBan"].ToString();
                liv.SubItems.Add(dbrow["TenBan"].ToString());
                liv.SubItems.Add(dbrow["TenKhu"].ToString());
                liv.SubItems.Add(dbrow["TenLoaiBan"].ToString());
                int tt = int.Parse(dbrow["TinhTrang"].ToString());
                if (tt == 0)
                {
                    liv.SubItems.Add("Trống");
                }
                if (tt == 1)
                {
                    liv.SubItems.Add("Đã Đặt");
                }
                if (tt == 2)
                {
                    liv.SubItems.Add("Đang Sử Dụng");
                }
                liv.SubItems.Add(dbrow["TenNH"].ToString());
                lvBan.Items.Add(liv);
            }
        }

        private int LayMaKhu(int maNH)
        {
            string tenkhu = cbbKhuVuc.Text;
            for (int i = 0; i < listKhuVuc.Count; i++)
            {
                if (tenkhu == listKhuVuc[i].TenKhu && listKhuVuc[i].MaNH == maNH)
                    return listKhuVuc[i].MaKhu;
            }
            return -1;
        }

        private int LayMaTinhTrang()
        {
            int tinhtrang = 0;
            if (cbbTinhTrang.Text == "Trống")
                tinhtrang = 0;
            if (cbbTinhTrang.Text == "Đã Đặt")
                tinhtrang = 1;
            if (cbbTinhTrang.Text == "Đang Sử Dụng")
                tinhtrang = 3;
            return tinhtrang;
        }

        private int LayMaLoaiBan()
        {
            int maLoai = 0;
            if (cbbLoaiBan.Text == "VIP")
                maLoai = 1;
            if (cbbLoaiBan.Text == "Thường")
                maLoai = 2;
            return maLoai;
        }

        private void LoadBan(int maNH, int maKhu, int loai, int tinhtrang)
        {
            try
            {
                DataTable dbtab;
                if (fix)
                    dbtab = bService.LoadBanDirtyread(maNH, maKhu, loai, tinhtrang);
                else
                    dbtab = bService.LoadBanDirtyreadError(maNH, maKhu, loai, tinhtrang);
                lvBan.Items.Clear();
                for (int i = 0; i < dbtab.Rows.Count; i++)
                {
                    DataRow dbrow = dbtab.Rows[i];
                    ListViewItem liv = new ListViewItem();
                    liv.Text = dbrow["MaBan"].ToString();
                    liv.SubItems.Add(dbrow["TenBan"].ToString());
                    liv.SubItems.Add(dbrow["TenKhu"].ToString());
                    liv.SubItems.Add(dbrow["TenLoaiBan"].ToString());
                    int tt = int.Parse(dbrow["TinhTrang"].ToString());
                    if (tt == 0)
                    {
                        liv.SubItems.Add("Trống");
                    }
                    if (tt == 1)
                    {
                        liv.SubItems.Add("Đã Đặt");
                    }
                    if (tt == 2)
                    {
                        liv.SubItems.Add("Đang Sử Dụng");
                    }
                    liv.SubItems.Add(dbrow["TenNH"].ToString());
                    lvBan.Items.Add(liv);
                }
            }
            catch (Exception)
            {
                return;
            }
        }

        private void LoadKhuTheoNH(int ma)
        {
            cbbKhuVuc.Items.Clear();
            DataTable dbtab = kvService.TimKVMaNH(ma);
            for (int i = 0; i < dbtab.Rows.Count; i++)
            {
                KhuVuc kv = new KhuVuc();
                DataRow dbrow = dbtab.Rows[i];
                kv.MaKhu = int.Parse(dbrow["MaKhu"].ToString());
                kv.MaNH = int.Parse(dbrow["MaNH"].ToString());
                kv.TenKhu = dbrow["TenKhu"].ToString();
                listKhuVuc.Add(kv);
                cbbKhuVuc.Items.Add(kv.TenKhu);
            }

            if (cbbKhuVuc.Items.Count > 0)
                cbbKhuVuc.Text = cbbKhuVuc.Items[0].ToString();
            else
            {
                cbbKhuVuc.Items.Clear();
                cbbKhuVuc.Text = "";
                MessageBox.Show("Nhà Hàng Đang Xây Dựng");
                return;
            }
        }

        private void LoadLoaiBan()
        {
            DataTable dbtab = bService.LoadLoaiBan();
            {
                for (int i = 0; i < dbtab.Rows.Count; i++)
                {
                    DataRow dbrow = dbtab.Rows[i];
                    cbbLoaiBan.Items.Add(dbrow["TenLoaiBan"].ToString());
                }
                cbbLoaiBan.Text = cbbLoaiBan.Items[0].ToString();
            }
        }

        private void LoadNH()
        {
            DataTable dbtab = nhService.LoadNH();
            for (int i = 0; i < dbtab.Rows.Count; i++)
            {
                DataRow dbrow = dbtab.Rows[i];
                cbbLocNH.Items.Add(dbrow["TenNH"].ToString());
            }
            cbbLocNH.Text = cbbLocNH.Items[0].ToString();
        }

        private void QLBan_Load(object sender, EventArgs e)
        {
            LoadNH();
            int maNH = nhService.LayMaNH(cbbLocNH.Text);
            LoadKhuTheoNH(maNH);
            LoadLoaiBan();
            LoadTinhTrang();
            int maKhu = LayMaKhu(maNH);
            int maLoai = LayMaLoaiBan();
            int tinhtrang = LayMaTinhTrang();
            if (maKhu != -1)
                LoadBan(maNH, maKhu, maLoai, tinhtrang);
            else
                return;
        }

    }
}
